package com.easybenefit.UUService.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.easybeanfit.UUService.commutill.UUViewHolder;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.model.GridViewBean;

import java.util.List;

/**
 * 主界面gridview  adapter
 * Created by Mrkin on 2016/6/9.
 */
public class MainGridViewAdapter extends BaseAdapter {
    private List<GridViewBean> list;
    private Context context;

    public MainGridViewAdapter(List<GridViewBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_main_gridview, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image_gridview_item);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name_gridview_item);
            viewHolder.tv_state = (TextView) convertView.findViewById(R.id.tv_state_gridview_item);
            viewHolder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.imageView.setImageDrawable(list.get(position).getImageRes());
        convertView.setBackgroundColor(context.getResources().getColor(list.get(position).getGridBackColorRes()));
        viewHolder.tv_name.setText(list.get(position).getName());
        viewHolder.tv_state.setText(list.get(position).getCode() + "");
        if (list.get(position).getNumber() == 0) {
            viewHolder.tv_number.setVisibility(View.GONE);
        } else {
            viewHolder.tv_number.setText(list.get(position).getNumber()+"");
            viewHolder.tv_number.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    class ViewHolder {
        public ImageView imageView;

        public TextView tv_name;

        public TextView tv_state;

        public TextView tv_number;
    }
}
